package net.jiastudy.biz.sys.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import com.baomidou.mybatisplus.mapper.EntityWrapper;

import net.jiastudy.biz.sys.entity.Dict;
import net.jiastudy.biz.sys.mapper.DictMapper;
import net.jiastudy.biz.sys.service.DictService;
import net.jiastudy.biz.user.entity.User;
import net.jiastudy.core.base.BaseService;
import net.jiastudy.core.exception.ServiceException;
import net.jiastudy.core.utils.BeanUtils;

@Service
@Transactional(rollbackFor = Exception.class)
public class DictServiceImpl extends BaseService<DictMapper, Dict> implements DictService {

	@Override
	public Dict createDict(Dict dict) {
		if (this.checkDictKeyExists(dict.getGroupCode(), dict.getDictKey(), null)) {
			throw new ServiceException("406", "字典key已经存在");
		}
		if (null == dict.getSeq()) {
			dict.setSeq(this.getNextSeqByGroupCode(dict.getGroupCode()));
		}
		if (null == dict.getDictKey()) {
			dict.setDictKey(dict.getSeq().toString());
		}
		User currentUser = this.getCurrentUser();
		dict.setCreatedUserId(null != currentUser ? currentUser.getId() : null);
		if (this.insert(dict)) {
			return this.selectById(dict.getId());
		}
		return null;
	}

	@Override
	public Dict updateDict(Dict dict) {
		Dict dbdict = this.selectById(dict.getId());
		if (this.checkDictKeyExists(dict.getGroupCode(), dict.getDictKey(), dict.getId())) {
			throw new ServiceException("406", "字典key已经存在");
		}
		BeanUtils.copyPropertiesWithIgnoreProperties(dict, dbdict, "id", "groupCode", "createDate", "lastModified");
		if (this.updateById(dict)) {
			return this.selectById(dict.getId());
		}
		return null;
	}

	@Override
	public boolean checkDictKeyExists(@Validated String groupCode, @Validated String dictKey, Long currentDictId) {
		if (null == dictKey) {
			return false;
		}
		Dict dict = this.getDictByGroupCodeAndKey(groupCode, dictKey);
		if (null == dict) {
			return false;
		}
		if (dict.getId().equals(currentDictId)) {
			return false;
		}
		return true;
	}

	@Override
	public boolean deleteDict(String groupCode, String dictKey) {
		EntityWrapper<Dict> ew = new EntityWrapper<>();
		ew.eq("group_code", groupCode).eq("dict_key", dictKey);
		return this.delete(ew);
	}

	@Override
	public Dict getDictByGroupCodeAndKey(String groupCode, String dictKey) {
		EntityWrapper<Dict> ew = new EntityWrapper<>();
		ew.eq("group_code", groupCode).eq("dict_key", dictKey);
		return this.selectOne(ew);
	}

	@Override
	public List<Dict> findDictsByGroupCode(String groupCode) {
		return this.findDictsByGroupCode(groupCode, true);
	}

	@Override
	public List<Dict> findDictsByGroupCode(String groupCode, boolean isAsc) {
		EntityWrapper<Dict> ew = new EntityWrapper<>();
		ew.eq("group_code", groupCode);
		ew.orderBy("seq", isAsc);
		return this.selectList(ew);
	}

	@Override
	public Integer getNextSeqByGroupCode(String groupCode) {
		return this.baseMapper.getNextSeqByGroupCode(groupCode);
	}

	@Override
	public Dict getDictById(Long id) {
		return this.selectById(id);
	}

	@Override
	public List<Map<String, String>> findDictKv(String groupCode, boolean isAsc) {
		List<Dict> dicts = this.findDictsByGroupCode(groupCode, isAsc);
		List<Map<String, String>> kvs = new ArrayList<>();
		for (Dict dict : dicts) {
			Map<String, String> map = new HashMap<>();
			map.put("key", dict.getDictKey());
			map.put("value", dict.getDictValue());
			kvs.add(map);
		}
		return kvs;
	}

}
